﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Zombie_Demo.Utilities;

namespace Zombie_Demo.World
{
    public static class PageSystem
    {
        private static Dictionary<Point, MapBlock> page;
        private static Texture2D[] mapTextures;
        private static Random r;

        public static Dictionary<Point, MapBlock> Page
        {
            get { return page; }
        }

        public static void Init()
        {
            r = new Random();

            page = new Dictionary<Point, MapBlock>();
            MapBlock firstBlock = new MapBlock(new Point(0, 0));
            firstBlock.RandomDirt(r);
            firstBlock.RandomWater(r);
            page.Add(firstBlock.WorldCordinate, firstBlock);
            mapTextures = new Texture2D[7];
            mapTextures[0] = ContentLibrary.GetTexture("Dirt_1");
            mapTextures[1] = ContentLibrary.GetTexture("Dirt_2");
            mapTextures[2] = ContentLibrary.GetTexture("Dirt_3");
            mapTextures[3] = ContentLibrary.GetTexture("Dirt_4");
            mapTextures[4] = ContentLibrary.GetTexture("Dirt_5");
            mapTextures[5] = ContentLibrary.GetTexture("Water_1");
            mapTextures[6] = ContentLibrary.GetTexture("Water_2");
        }

        public static void Draw(SpriteBatch spriteBatch, Rectangle cameraBounds)
        {
            foreach (KeyValuePair<Point, MapBlock> pair in page)
            {
                for(int x = 0; x < pair.Value.TileData.GetLength(0); x++)
                    for (int y = 0; y < pair.Value.TileData.GetLength(1); y++)
                    {
                        if (new Rectangle(pair.Value.WorldCordinate.X + (x * 50),
                            pair.Value.WorldCordinate.Y + (y * 50), 50, 50).Intersects(cameraBounds))
                        {
                            spriteBatch.Draw(mapTextures[(int)pair.Value.TileData[x, y]],
                                new Vector2(pair.Value.WorldCordinate.X + (x * 50),
                                pair.Value.WorldCordinate.Y + (y * 50)), Color.White);
                        }
                    }
            }
        }


        public static void Update(Point playerPosition)
        {
            Point playerBlockPosition = new Point(Math.Abs(playerPosition.X) / 3200, Math.Abs(playerPosition.Y) / 3200);

            if (playerPosition.X < 0)
                playerBlockPosition.X = (playerBlockPosition.X * -1) - 1;

            if (playerPosition.Y < 0)
                playerBlockPosition.Y = (playerBlockPosition.Y * -1) - 1;

            Point adjustment = new Point(playerBlockPosition.X * 3200, playerBlockPosition.Y * 3200);
            NeighborBlockInfo nbi = page[adjustment].NBI;

            if (!nbi.North)
            {
                Point p = new Point(playerBlockPosition.X * 3200, playerBlockPosition.Y * 3200 + 3200);
                if (!page.ContainsKey(p))
                {
                    MapBlock b = new MapBlock(p);
                    b.RandomDirt(r);
                    page.Add(p, b);
                }
                nbi.North = true;
            }
            if (!nbi.NorthEast)
            {
                Point p = new Point(playerBlockPosition.X * 3200 + 3200, playerBlockPosition.Y * 3200 +3200);
                if (!page.ContainsKey(p))
                {
                    MapBlock b = new MapBlock(p);
                    b.RandomDirt(r);
                    page.Add(p, b);
                }
                nbi.NorthEast = true;
            } 
            if (!nbi.East)
            {
                Point p = new Point(playerBlockPosition.X * 3200 + 3200, playerBlockPosition.Y * 3200);
                if (!page.ContainsKey(p))
                {
                    MapBlock b = new MapBlock(p);
                    b.RandomDirt(r);
                    page.Add(p, b);
                }
                nbi.East = true;
            }
            if (!nbi.SouthEast)
            {
                Point p = new Point(playerBlockPosition.X * 3200 - 3200, playerBlockPosition.Y * 3200 + 3200);
                if (!page.ContainsKey(p))
                {
                    MapBlock b = new MapBlock(p);
                    b.RandomDirt(r);
                    page.Add(p, b);
                }
                nbi.SouthEast = true;
            }
            if (!nbi.South)
            {
                Point p = new Point(playerBlockPosition.X * 3200 - 3200, playerBlockPosition.Y * 3200);
                if (!page.ContainsKey(p))
                {
                    MapBlock b = new MapBlock(p);
                    b.RandomDirt(r);
                    page.Add(p, b);
                }
                nbi.South = true;
            }
            if (!nbi.SouthWest)
            {
                Point p = new Point(playerBlockPosition.X * 3200 - 3200, playerBlockPosition.Y * 3200 - 3200);
                if (!page.ContainsKey(p))
                {
                    MapBlock b = new MapBlock(p);
                    b.RandomDirt(r);
                    page.Add(p, b);
                }
                nbi.SouthWest = true;
            }
            if (!nbi.West)
            {
                Point p = new Point(playerBlockPosition.X * 3200, playerBlockPosition.Y * 3200 - 3200);
                if (!page.ContainsKey(p))
                {
                    MapBlock b = new MapBlock(p);
                    b.RandomDirt(r);
                    page.Add(p, b);
                }
                nbi.West = true;
            }
            if (!nbi.NorthWest)
            {
                Point p = new Point(playerBlockPosition.X * 3200 + 3200, playerBlockPosition.Y * 3200 - 3200);
                if (!page.ContainsKey(p))
                {
                    MapBlock b = new MapBlock(p);
                    b.RandomDirt(r);
                    page.Add(p, b);
                }
                nbi.NorthWest = true;
            }
            page[adjustment].NBI = nbi;
        }
    }
}
